/*
 * User.java
 *
 * Created on January 4, 2008, 11:23 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class WispUser {
    private dbConnection dbcon = new dbConnection();
    private String username, password, eAdd, dateJoined, gender;
    private String userId;
    /** Creates a new instance of User */
    public WispUser() {
    }
    
    public WispUser(String uname, String pword, String emailAd, String dJoined, String ugender, String id){
        setUsername(uname);
        setPassword(pword);
        setEAdd(emailAd);
        setDateJoined(dJoined);
        setGender(ugender);
        setUserId(id);
    }
    public boolean checkUsernameExist(String uname) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM WispUser WHERE username = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, uname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
     public boolean checkPasswordExist(String pword) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM WispUser WHERE password = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, pword);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
     
    public boolean checkUserIdExist(String id) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
        
        System.out.println("hi! im in check user id");
        getDbcon().connect();
        query = "SELECT userId FROM WispUser WHERE userId = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, id);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
            }
        System.out.println("hi");
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
      public void addUser (WispUser u){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        if (!checkUsernameExist(u.getUsername()) && !checkPasswordExist(u.getPassword()))
        {
            System.out.println("hi! im in adduser!");
            getDbcon().connect();
            query = "INSERT INTO WispUser (username, password, uEmail, dateJoined, gender, userId) VALUES (?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, u.getUsername());
                stmt.setString(2, u.getPassword());
                stmt.setString(3, u.getEAdd());
                System.out.println(u.getDateJoined());
                stmt.setString(4, u.getDateJoined());
                stmt.setString(5, u.getGender());
                stmt.setString(6, u.getUserId());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
      public boolean validateUser(String uname, String pword) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM WispUser WHERE username = ? AND password = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, uname);
            stmt.setString(2, pword);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
      
      public void viewUserInfoViaID (String id){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT username, password, uEmail, dateJoined, gender, userId FROM WispUser WHERE userId = ?";
        
        try
        {
            // 
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, id);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                this.setUsername(rs.getString("username").trim());
                this.setPassword(rs.getString("password").trim());
                this.setDateJoined(rs.getString("dateJoined").trim());
                this.setGender(rs.getString("gender").trim());
                setUserId(rs.getString("userId"));
             }
            //crset.populate(rs);
            rs.close(); 					// close the result
            stmt.close();
           // return crset; // close the statement					// close the statement
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
        }
     }
      
      public void viewUserInfo (String uname){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT username, password, uEmail, dateJoined, gender, userId FROM WispUser WHERE username = ?";
        
        try
        {
            // 
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, uname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                this.setUsername(rs.getString("username").trim());
                this.setPassword(rs.getString("password").trim());
                this.setDateJoined(rs.getString("dateJoined").trim());
                this.setGender(rs.getString("gender").trim());
                setUserId(rs.getString("userId"));
             }
            //crset.populate(rs);
            rs.close(); 					// close the result
            stmt.close();
           // return crset; // close the statement					// close the statement
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
        }
     }
      
      public String viewByUserId (Long id){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        String sId = id.toString();
        String name = "";
        query = "SELECT username, password, uEmail, dateJoined, gender, userId FROM WispUser WHERE userId = ?";
        
        try
        {
            // 
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, sId);
            rs = stmt.executeQuery();	
            
            while (rs.next()) 
            {	
                name = rs.getString("username").trim();
             }
            //crset.populate(rs);
            rs.close(); 					// close the result
            stmt.close();
           // return crset; // close the statement					// close the statement
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
            return name;
        }
     }
      
      public void editPassword (String uname, String oPword, String nPword) {
         						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        if (validateUser(uname, oPword))
        {
            getDbcon().connect();
            query = "UPDATE WispUser SET password = ? WHERE username = ?";
            try 
            {

                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, nPword);
                stmt.setString(2, uname);
                stmt.executeUpdate();	
                stmt.close();					
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());	       	
            }
            getDbcon().disconnect();
        }
     }

      public void editUserId (String uname, String id) {
         						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

       
            getDbcon().connect();
            query = "UPDATE WispUser SET userId = ? WHERE username = ?";
            try 
            {

                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, id);
                stmt.setString(2, uname);
                stmt.executeUpdate();	
                stmt.close();					
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());	       	
            }
            getDbcon().disconnect();
        
     }
    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEAdd() {
        return eAdd;
    }

    public void setEAdd(String eAdd) {
        this.eAdd = eAdd;
    }

    public String getDateJoined() {
        return dateJoined;
    }

    public void setDateJoined(String dateJoined) {
        this.dateJoined = dateJoined;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
     public void viewId (String uname, String oPword){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT userId FROM WispUser WHERE username = ? AND password = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, uname);
            stmt.setString(2, oPword);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                setUserId(rs.getString("userId"));
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally 
        {
            getDbcon().disconnect();
        }
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
